/*
 * disasm.c
 *
 * Copyright 2024 dh33ex <dh33ex@riseup.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA or visit <http://www.gnu.org/licenses/>.
 *
 *
 */

#define MEMORY_SIZE 0x480

#define  MOV    0
#define  AND    1
#define  OR     2
#define  XOR    3
#define  NOT    4
#define  ADD    5
#define  SUB    6
#define  MUL    7
#define  DIV    8

#define  LSL    9
#define  ASR   10
#define  ROT   11

#define LDW     0
#define LDB     1
#define STW     2
#define STB     3

#define ALL     0
#define MI     1
#define EQ     2
#define CS     3
#define VS     4
#define PL     5
#define NE     6
#define CC     7
#define VC     8
#define GE     9
#define LT    10

#include <stdio.h>
#include <stdlib.h>

unsigned int memory[MEMORY_SIZE];


int disasm(int i) {
    unsigned int IR = memory[i];

    if (IR == 0) {
        return 1;
    }

    unsigned int u_flag = (IR & (1 << 29)) >> 29;
    unsigned int v_flag = (IR & (1 << 28)) >> 28;

    unsigned int reg1 = (IR & (15 << 24)) >> 24;
    unsigned int reg2 = (IR & (15 << 20)) >> 20;
    unsigned int reg3 = IR & 15;
    unsigned int op = (IR & (15 << 16)) >> 16;
    unsigned int im = IR & 65535;
    if (v_flag) {
        im = -((~im & 65535) - 1);
    }

    int offset_3 = IR & 1048575;
    if (offset_3 & (1 << 19)) {
        offset_3 = -((~offset_3) & 1048575) - 1;
    }

    int offset_4 = IR & 16777215;
    if (offset_4 & (1 << 23)) {
        offset_4 = -((~offset_4) & 16777215) - 1;
    }
    unsigned int cond = (IR & (15 << 24)) >> 24;

    unsigned int format = (IR & (3 << 30)) >> 30;

    printf(" %8x | %3d | ", IR, i*4);
    switch (format) {
        case 0:
            switch (op) {
                case MOV:
                printf("MOV r%d r%d\n", reg1, reg3);
                break;

                case AND:
                printf("AND r%d r%d r%d\n", reg1, reg2, reg3);
                break;

                case OR:
                printf("OR  r%d r%d r%d\n", reg1, reg2, reg3);
                break;

                case XOR:
                printf("XOR r%d r%d r%d\n", reg1, reg2, reg3);
                break;

                case LSL:
                printf("LSL r%d r%d r%d\n", reg1, reg2, reg3);
                break;

                case ASR:
                printf("ASR r%d r%d r%d\n", reg1, reg2, reg3);
                break;

                case ROT:
                printf("ROT r%d r%d r%d\n", reg1, reg2, reg3);
                break;

                case NOT:
                printf("NOT r%d r%d\n", reg1, reg3);
                break;

                case ADD:
                printf("ADD r%d r%d r%d\n", reg1, reg2, reg3);
                break;

                case SUB:
                printf("SUB r%d r%d r%d\n", reg1, reg2, reg3);
                break;

                case MUL:
                printf("MUL r%d r%d r%d\n", reg1, reg2, reg3);
                break;

                case DIV:
                printf("DIV r%d r%d r%d\n", reg1, reg2, reg3);
                break;

                default:
                printf("invalide F1 command\n");
                break;
            }
            break;

        case 1:
            /* v considiration not implemented */
            switch (op) {
                case MOV:
                printf("MOV r%d %d\n", reg1, im);
                break;

                case AND:
                printf("AND r%d r%d %d\n", reg1, reg2, im);
                break;

                case OR:
                printf("OR  r%d r%d %d\n", reg1, reg2, im);
                break;

                case XOR:
                printf("XOR r%d r%d %d\n", reg1, reg2, im);
                break;

                case LSL:
                printf("LSL r%d r%d %d\n", reg1, reg2, im);
                break;

                case ASR:
                printf("ASR r%d r%d %d\n", reg1, reg2, im);
                break;

                case ROT:
                printf("ROT r%d r%d %d\n", reg1, reg2, im);

                case ADD:
                printf("ADD r%d r%d %d\n", reg1, reg2, im);
                break;

                case SUB:
                printf("SUB r%d r%d %d\n", reg1, reg2, im);
                break;

                case MUL:
                printf("MUL r%d r%d %d\n", reg1, reg2, im);
                break;

                case DIV:
                printf("DIV r%d r%d %d\n", reg1, reg2, im);
                break;

                default:
                printf("invalide F2 command\n");
                break;
            }
            break;

        case 2:
            /* byte manipulation is not implemented (v) */
            if (u_flag) {
                printf("ST  r%d r%d 0x%x\n", reg1, reg2, offset_3);
            } else {
                printf("LD  r%d r%d 0x%x\n", reg1, reg2, offset_3);
            }
            break;

        case 3:
            switch (cond) {
                case ALL:
                printf("%s", (v_flag ? "BRA" : "JMP"));
                break;

                case MI:
                printf("%sMI", (v_flag ? "B" : "J"));
                break;

                case EQ:
                printf("%sEQ", (v_flag ? "B" : "J"));
                break;

                case PL:
                printf("%sPL", (v_flag ? "B" : "J"));
                break;

                case NE:
                printf("%sNE", (v_flag ? "B" : "J"));
                break;

                case CS:
                printf("%sCS", (v_flag ? "B" : "J"));
                break;

                case VS:
                printf("%sVS", (v_flag ? "B" : "J"));
                break;

                case CC:
                printf("%sCC", (v_flag ? "B" : "J"));
                break;

                case VC:
                printf("%sVC", (v_flag ? "B" : "J"));
                break;

                case GE:
                printf("%sGE", (v_flag ? "B" : "J"));
                break;

                case LT:
                printf("%sLT", (v_flag ? "B" : "J"));
                break;

                default:
                printf("(invalide F4 command condition) ");
                break;
            }
            print(" %s%d\n", (u_flag ? "" : "r"), (u_flag ? offset_4 : reg3));
            break;

        default:
        printf("format error!\n");
        break;
    }
    return 1;
}


void read_program(char *filename) {
    FILE *byte_file = fopen(filename, "r");

    char *line = NULL;
    size_t len = 0;
    unsigned int i = 0;

    if (byte_file == NULL) {
        printf("can't open program\n");
        exit(1);
    }

    while (getline(&line, &len, byte_file) != -1) {
        memory[i++] = atoi(line);
    }

    fclose(byte_file);

    if (line) {
        free(line);
    }
}


int main(int argc, char **argv) {
    if (argc == 2) {
        read_program(argv[1]);
    } else {
        printf("%s <file.txt>\n", argv[0]);
        return 1;
    }

    int i = 0;
    printf("----------+-----+----------------\n");
    printf("   word   |  №  | instruction\n");
    printf("----------+-----+----------------\n");
    while (disasm(i++) && i < MEMORY_SIZE) {};
    printf("----------+-----+----------------\n");
	return 0;
}
